﻿using HostsTool.Currency.IoUtils;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HostsTool.Currency
{
    internal class ListHelper
    {
        /// <summary>
        /// List去重复
        /// </summary>
        /// <param name="OldList"></param>
        /// <returns></returns>
        public List<string> ListRepeat(List<string> OldList)
        {
            List<string> ToRepeat = new List<string>();
            int Count = 0;
            foreach (string line in OldList)
            {
                new PrintLog().PercentLog(OldList.Count, ++Count);
                if (!TextHelper.JudgeNull(line) && !ToRepeat.Contains(line) && line != "")
                    ToRepeat.Add(line);
            }
            return ToRepeat;
        }

        /// <summary>
        /// 替换整个List里的内容  增强模式，使用正则
        /// </summary>
        /// <param name="RepList"></param>
        /// <param name="ReplaceForm"></param>
        /// <param name="ReplaceTo"></param>
        /// <returns></returns>
        public List<string> RepListEnhanceHelper(List<string> RepList, string ReplaceForm, string ReplaceTo)
        {
            List<string> TempList = new List<string>();
            foreach (string Line in RepList)
            {
                if (Line == "")
                    continue;
                TempList.Add(TextHelper.ReplaceText(Line, ReplaceForm, ReplaceTo));
            }

            return TempList;
        }

        /// <summary>
        /// 逐行判断是否包含屏蔽词，如果包含则移除。
        /// </summary>
        /// <param name="TextList"></param>
        /// <param name="ExcludeList"></param>
        /// <returns></returns>
        public List<string> ListRemoveExclude(List<string> TextList, List<string> ExcludeList)
        {
            List<string> TempList = new List<string>();
            int Count = 0;
            foreach (string Line in TextList)
            {
                new PrintLog().PercentLog(TextList.Count, ++Count);
                bool ContinueFlag = false;
                foreach (string Exclude in ExcludeList)
                {
                    if (Exclude != "" && Line.IndexOf(Exclude) >= 0)
                    {
                        Console.WriteLine("捕获到关键词" + Exclude);

                        ContinueFlag = true;
                        break;
                    }
                }
                if (ContinueFlag)
                    continue;
                TempList.Add(Line);
            }
            return TempList;
        }

        /// <summary>
        /// 切割list，返回最后面的的指定数量
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="NarrowCount"></param>
        /// <returns></returns>
        public List<string> ListNarrow(String FileName, int NarrowCount)
        {
            List<string> FileAllLine = FileHelper.ReadAllLine(FileName);
            FileAllLine = FileAllLine.Skip(FileAllLine.Count - NarrowCount).ToList<string>();
            Console.WriteLine("日志处理后剩余{0}行", FileAllLine.Count);
            return FileAllLine;
        }

        /// <summary>
        /// 按指定数字切割list，并返回封装的List
        /// </summary>
        /// <param name="DataList"></param>
        /// <param name="SkipCount"></param>
        /// <returns></returns>
        public List<List<string>> ListSkip(List<string> DataList, int SkipCount)
        {
            List<List<string>> TempData = new List<List<string>>();

            int size = (int)Math.Ceiling((decimal)DataList.Count / SkipCount);
            for (int i = 1; i <= size; i++)
            {
                TempData.Add(DataList.Skip((i - 1) * SkipCount).Take(SkipCount).ToList());
            }

            return TempData;
        }

        /// <summary>
        /// 指定一个规则 替换整个LIST里的内容
        /// </summary>
        /// <param name="TextList"></param>
        /// <param name="RegexPattern"></param>
        /// <returns></returns>
        internal List<string> ReplaceList(List<string> TextList, string RegexPattern, String NewText = "")
        {
            List<string> ImgTemp = new List<string>();

            foreach (string Line in TextList)
            {
                if (Line == "")
                    continue;
                ImgTemp.Add(TextHelper.ReplaceText(Line, RegexPattern, NewText));
            }
            return ImgTemp;
        }

        /// <summary>
        /// 配合下方的类使用，记录已经使用过的值
        /// </summary>
        private static List<int> RandomList = new List<int>();

        /// <summary>
        /// 从LIST里随机抽取一个结果返回
        /// </summary>
        /// <param name="TextList"></param>
        /// <param name="NoRepeat">为true则启动去重复</param>
        /// <param name="FirstFlag">为true则清理上次结果</param>
        /// <returns></returns>
        public string Ramdom(List<string> TextList, bool NoRepeat, bool FirstFlag = false)
        {
            int Random = new Random().Next(TextList.Count);
            if (FirstFlag)
                RandomList = new List<int>();

            if (NoRepeat && TextList.Count != 0)
            {
                if (RandomList.Contains(Random))
                {
                    Console.WriteLine("重复值" + Random);
                    GC.Collect();
                    return Ramdom(TextList, NoRepeat);
                }
                RandomList.Add(Random);
            }
            if (TextList.Count == 0)
                return "";

            return TextList[Random];
        }

        /// <summary>
        ///  List判断在一个子集里  如果存在返回true，不存在返回false
        /// </summary>
        /// <param name="Sublist">子集 </param>
        /// <param name="ParentList">父集</param>
        /// <returns></returns>
        public bool JudgeRepetition(List<string> Sublist, List<string> ParentList)
        {
            Sublist = new ListHelper().ListRepeat(Sublist);
            ParentList = new ListHelper().ListRepeat(ParentList);

            foreach (string line in Sublist)
            {
                if (ParentList.Contains(line))
                {
                    Console.WriteLine("屏蔽词：" + line);
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断字符串列表里是否包含关键词，返回不包含关键词的列表
        /// </summary>
        /// <param name="StrList"></param>
        /// <param name="ExcluedeList"></param>
        /// <returns></returns>
        public List<string> JudgeRepetitionWithKey(List<string> StrList, List<string> ExcluedeList)
        {
            List<string> TempList = new List<string>();
            foreach (var item in StrList)
            {
                if (item == "")
                    continue;
                if (!JudgeRepetitionWithKey(item, ExcluedeList))
                    TempList.Add(item);
            }
            return TempList;
        }

        /// <summary>
        /// 判断字符串中是否包含关键词，包含关键词返回true
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="ExcluedeList"></param>
        /// <returns>包含关键词返回true</returns>
        public bool JudgeRepetitionWithKey(string Str, List<string> ExcluedeList)
        {
            foreach (var item in ExcluedeList)
            {
                if (item == "")
                    continue;
                if (Str.IndexOf(item) > 0)
                {
                    Console.WriteLine("屏蔽词：" + item);
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 获取列表的第一个值
        /// </summary>
        /// <param name="ValueList"></param>
        /// <returns></returns>
        public string GetFirst(List<string> ValueList)
        {
            if (ValueList.Count > 0)
                return ValueList[0];
            else
                return "";
        }
    }
}